{{- if (include "mongodb.arbiter.enabled" .) }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ printf "%s-arbiter" (include "mongodb.fullname" .) }}
  namespace: {{ include "mongodb.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: arbiter
    {{- if .Values.arbiter.labels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.labels "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.arbiter.annotations .Values.commonAnnotations }}
  annotations:
    {{- if .Values.arbiter.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.arbiter.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  serviceName: {{ include "mongodb.arbiter.service.nameOverride" . }}
  podManagementPolicy: {{ .Values.arbiter.podManagementPolicy }}
  {{- if .Values.arbiter.updateStrategy }}
  updateStrategy: {{- toYaml .Values.arbiter.updateStrategy | nindent 4 }}
   {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: arbiter
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: arbiter
        {{- if .Values.arbiter.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if or (include "mongodb.arbiter.createConfigmap" .) .Values.arbiter.podAnnotations }}
      annotations:
        {{- if (include "mongodb.arbiter.createConfigmap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/arbiter/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.arbiter.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "mongodb.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.arbiter.schedulerName }}
      schedulerName: {{ .Values.arbiter.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ template "mongodb.serviceAccountName" . }}
      {{- if .Values.arbiter.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.arbiter.podAffinityPreset "component" "arbiter" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.arbiter.podAntiAffinityPreset "component" "arbiter" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.arbiter.nodeAffinityPreset.type "key" .Values.arbiter.nodeAffinityPreset.key "values" .Values.arbiter.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.arbiter.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.arbiter.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.arbiter.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.arbiter.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.arbiter.priorityClassName }}
      priorityClassName: {{ .Values.arbiter.priorityClassName }}
      {{- end }}
      {{- if .Values.arbiter.runtimeClassName }}
      runtimeClassName: {{ .Values.arbiter.runtimeClassName }}
      {{- end }}
      {{- if .Values.arbiter.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.arbiter.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{ if .Values.arbiter.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.arbiter.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.arbiter.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.tls.enabled .Values.arbiter.enabled }}
        - name: generate-client
          image: {{ include "mongodb.tls.image" . }}
          imagePullPolicy: {{ .Values.tls.image.pullPolicy | quote }}
          env:
            - name: MY_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: MY_POD_HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
          volumeMounts:
          - name: certs-volume
            mountPath: /certs/CAs
          - name: certs
            mountPath: /certs
          - name: common-scripts
            mountPath: /bitnami/scripts
          command:
            - /bitnami/scripts/generate-certs.sh
          args:
            - -s {{ include "mongodb.arbiter.service.nameOverride" . }}
        {{- end }}
      containers:
        - name: mongodb-arbiter
          image: {{ include "mongodb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.arbiter.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.arbiter.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.arbiter.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.arbiter.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.arbiter.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MY_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: K8S_SERVICE_NAME
              value: "{{ include "mongodb.arbiter.service.nameOverride" . }}"
            - name: MONGODB_REPLICA_SET_MODE
              value: "arbiter"
            - name: MONGODB_INITIAL_PRIMARY_HOST
              value: {{ printf "%s-0.%s.$(MY_POD_NAMESPACE).svc.%s" (include "mongodb.fullname" .) (include "mongodb.service.nameOverride" .) .Values.clusterDomain }}
            - name: MONGODB_REPLICA_SET_NAME
              value: {{ .Values.replicaSetName | quote }}
            - name: MONGODB_ADVERTISED_HOSTNAME
              value: "$(MY_POD_NAME).$(K8S_SERVICE_NAME).$(MY_POD_NAMESPACE).svc.{{ .Values.clusterDomain }}"
            - name: MONGODB_PORT_NUMBER
              value: {{ .Values.arbiter.containerPorts.mongodb | quote }}
            {{- if .Values.auth.enabled }}
            - name: MONGODB_INITIAL_PRIMARY_ROOT_USER
              value: {{ .Values.auth.rootUser | quote }}
            - name: MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongodb.secretName" . }}
                  key: mongodb-root-password
            - name: MONGODB_REPLICA_SET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongodb.secretName" . }}
                  key: mongodb-replica-set-key
            {{- end }}
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "no" "yes" .Values.auth.enabled | quote }}
            {{- $extraFlags := .Values.arbiter.extraFlags | join " " -}}
            {{- if and .Values.tls.enabled .Values.arbiter.enabled  }}
              {{- $extraFlags = printf "--tlsMode=%s --tlsCertificateKeyFile=/certs/mongodb.pem --tlsCAFile=/certs/mongodb-ca-cert %s" .Values.tls.mode $extraFlags  }}
            {{- end }}
            {{- if ne $extraFlags "" }}
            - name: MONGODB_EXTRA_FLAGS
              value: {{ $extraFlags | quote }}
            {{- end }}
            {{- if and .Values.tls.enabled  .Values.arbiter.enabled }}
            - name: MONGODB_CLIENT_EXTRA_FLAGS
              value: --tls --tlsCertificateKeyFile=/certs/mongodb.pem --tlsCAFile=/certs/mongodb-ca-cert
            {{- end }}
            {{- if .Values.arbiter.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.arbiter.extraEnvVarsCM .Values.arbiter.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.arbiter.extraEnvVarsCM }}
            - configMapRef:
                name: {{ tpl .Values.arbiter.extraEnvVarsCM . | quote }}
            {{- end }}
            {{- if .Values.arbiter.extraEnvVarsSecret }}
            - secretRef:
                name: {{ tpl .Values.arbiter.extraEnvVarsSecret . | quote }}
            {{- end }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.arbiter.containerPorts.mongodb }}
              name: mongodb
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.arbiter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.arbiter.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- else if .Values.arbiter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.arbiter.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.arbiter.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- else if .Values.arbiter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.arbiter.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.arbiter.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- else if .Values.arbiter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.arbiter.resources }}
          resources: {{- toYaml .Values.arbiter.resources | nindent 12 }}
          {{- end }}
          {{- if or .Values.arbiter.configuration .Values.arbiter.existingConfigmap .Values.arbiter.extraVolumeMounts .Values.tls.enabled }}
          volumeMounts:
            {{- if or .Values.arbiter.configuration .Values.arbiter.existingConfigmap }}
            - name: config
              mountPath: /opt/bitnami/mongodb/conf/mongodb.conf
              subPath: mongodb.conf
            {{- end }}
            {{- if and .Values.tls.enabled .Values.arbiter.enabled }}
            - name: certs
              mountPath: /certs
            {{- end }}
            {{- if .Values.arbiter.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
        {{- if .Values.arbiter.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if or .Values.arbiter.configuration .Values.arbiter.existingConfigmap .Values.arbiter.extraVolumes .Values.tls.enabled }}
      volumes:
        - name: common-scripts
          configMap:
            name: {{ printf "%s-common-scripts" (include "mongodb.fullname" .) }}
            defaultMode: 0555
        {{- if or .Values.arbiter.configuration .Values.arbiter.existingConfigmap }}
        - name: config
          configMap:
            name: {{ include "mongodb.arbiter.configmapName" . }}
        {{- end }}
        {{- if and .Values.tls.enabled .Values.arbiter.enabled }}
        - name: certs
          emptyDir: {}
        - name: certs-volume
          secret:
            secretName: {{ template "mongodb.tlsSecretName" . }}
            items:
            - key: mongodb-ca-cert
              path: mongodb-ca-cert
              mode: 0600
            - key: mongodb-ca-key
              path: mongodb-ca-key
              mode: 0600
        {{- end }}
        {{- if .Values.arbiter.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.arbiter.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
{{- end }}
